/*
 * GoodsModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.google.gson.Gson;
import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.GoodsAboutRequest;
import com.yidiandao.business.data.request.GoodsDetailValueRequest;
import com.yidiandao.business.data.response.AddToShoppingCartResponse;
import com.yidiandao.business.data.response.BuyNowResponse;
import com.yidiandao.business.data.response.CategoryResponse;
import com.yidiandao.business.data.response.GoodsDetailResponse;
import com.yidiandao.business.data.response.GoodsDetailValueResponse;
import com.yidiandao.business.utils.GsonProvider;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 物品详情
 */
public class GoodsModel implements IGoodsModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getGoodsDetail(String tag, String goodsId, final onGetGoodsDataListener listener) {
        final GoodsAboutRequest request = new GoodsAboutRequest();
        request.setTag(tag);
        request.setGoodsId(goodsId);
        request.setUrl(Urls.GOODS_DETAIL);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                GoodsDetailResponse.class,
                new NetListener<GoodsDetailResponse>() {
                    @Override
                    public void onSuccess(GoodsDetailResponse goodsAboutResponse) {

                        GoodsDetailResponse.DataBean data = goodsAboutResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void addToCart(
            String tag, String goodsId, int count, final OnAddToShoppingCartListener listener) {
        GoodsAboutRequest request = new GoodsAboutRequest();
        request.setTag(tag);
        request.setGoodsId(goodsId);
        request.setUrl(Urls.ADD_TO_CART);
        request.setNumber(count + "");
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(
                request, AddToShoppingCartResponse.class,
                new NetListener<AddToShoppingCartResponse>() {
                    @Override
                    public void onSuccess(AddToShoppingCartResponse response) {
                        if (listener != null) {
                            AddToShoppingCartResponse.Data data = response.getData();
                            listener.onSuccess(
                                    data.getRecId(), data.getGoodsNumber(), data.getGoodsPrice());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void goodsCategory(String tag, final onGoodsCategoryListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setUrl(Urls.GOODS_CAT_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, CategoryResponse.class,
                new NetListener<CategoryResponse>() {
                    @Override
                    public void onSuccess(CategoryResponse categoryResponse) {
                        List<CategoryResponse.DataBean> data = categoryResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void buyNow(String tag, String goodsId, int number, final onBuyNowListener listener) {

        GoodsAboutRequest request = new GoodsAboutRequest();
        request.setGoodsId(goodsId);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.BUY_NOW);
        request.setNumber(number + "");
        mNetBiz.sendGetRequest(request, BuyNowResponse.class, new NetListener<BuyNowResponse>() {
            @Override
            public void onSuccess(BuyNowResponse response) {
                BuyNowResponse.Data data = response.getData().get(0);
                Gson gson = GsonProvider.getExposeGson();

                if (listener != null) {
                    listener.onSuccess(gson.toJson(response.getData()), data.getRecId(), data.getGoodsNumber(), data.getGoodsPrice());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void getValueData(
            String tag,
            String goodsId,
            int type,
            int currentPage,
            final onGetDataListener listener) {
        final GoodsDetailValueRequest request = new GoodsDetailValueRequest();
        request.setTag(tag);
        request.setUrl(Urls.GOODS_COMMENT);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(currentPage);
        request.setPageSize(Common.PAGER_SIZE);
        request.setGoodsId(goodsId);
        request.setType(type);
        mNetBiz.sendGetRequest(request, GoodsDetailValueResponse.class,
                new NetListener<GoodsDetailValueResponse>() {
                    @Override
                    public void onSuccess(GoodsDetailValueResponse goodsDetailValueResponse) {
                        GoodsDetailValueResponse.Data data = goodsDetailValueResponse.getData();

                        ArrayList<GoodsDetailValueResponse.ValueBean> listBeen =
                                new ArrayList<>();
                        if (null != data) {
                            if (null != data.getList()) {
                                listBeen.addAll(data.getList());
                            }
                        }
                        if (listener != null) {
                            listener.onSuccess(goodsDetailValueResponse, listBeen);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
